import numpy as np
import csv
from datetime import datetime
from time import strftime



def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2009/REDCAPLUS_100Region/'
    
    file = filepath + '100reg_regressiondata.csv'
    regressiondata = build_data_list(file)    #[dist,grossout,grossin,vol]

    file = filepath + 'migration_100regs_format.csv'
    #file = filepath + 'census_migration_100regs.csv'
    formatdata = build_data_list(file)  #[orgid, destid, flow]

    file = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/100regs_eastid.csv'
    regid = build_data_list(file)

    regressiondataeast = []
    regressiondatawest = []

    formatdataeast = []
    formatdatawest = []
    
    i = 0
    for item in regressiondata:
        if (regid[formatdata[i,0], 1] == 0) and (regid[formatdata[i,1], 1] == 0) :
            regressiondatawest.append(item) #go west
            formatdatawest.append(formatdata[i])
        elif (regid[formatdata[i,0], 1] == 1) and (regid[formatdata[i,1], 1] == 1) :
            regressiondataeast.append(item) #go east
            formatdataeast.append(formatdata[i])
        i += 1
        
    filepath = 'C:/_DATA/migration_census_2009/REDCAPLUS_100Region/tmp/'
    headerstr = 'dist,f1,f2,p1,p2,vol'
    fileLoc = filepath + 'regressiondataeast.csv'
    np.savetxt(fileLoc, regressiondataeast, delimiter=',', header = headerstr, fmt = '%s')
    fileLoc = filepath + 'regressiondatawest.csv'
    np.savetxt(fileLoc, regressiondatawest, delimiter=',', header = headerstr, fmt = '%s')

    headerstr = 'orgid,destid,flow'
    fileLoc = filepath + 'formatdataeast.csv'
    np.savetxt(fileLoc, formatdataeast, delimiter=',', header = headerstr, fmt = '%s')
    fileLoc = filepath + 'formatdatawest.csv'
    np.savetxt(fileLoc, formatdatawest, delimiter=',', header = headerstr, fmt = '%s')
    
    